// =======================================================================
// 
//       Filename:  popen2.cpp
// 
//    Description:  
// 
//        Version:  1.0
//        Created:  11.04.2010 21:37:56
//       Revision:  none
//       Compiler:  g++
// 
//         Author:  Dominik 'domachine' Burgdörfer (-), dominik.burgdoerfer@googlemail.com
//        Company:  -
// 
// =======================================================================

#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include "popen2.hpp"

namespace webplodder {
    namespace process {
        pid_t popen2(const char *command, int *infp, int *outfp)
        {
            int p_stdin[2], p_stdout[2];
            pid_t pid;

            if (pipe(p_stdin) != 0 || pipe(p_stdout) != 0)
                return -1;

            pid = fork();

            if (pid < 0)
                return pid;
            else if (pid == 0)
            {
                close(p_stdin[WRITE]);
                dup2(p_stdin[READ], READ);
                close(p_stdout[READ]);
                dup2(p_stdout[WRITE], WRITE);

                execl("/bin/sh", "sh", "-c", command, NULL);
                perror("execl");
                exit(1);
            }

            if (infp == NULL)
                close(p_stdin[WRITE]);
            else
                *infp = p_stdin[WRITE];

            if (outfp == NULL)
                close(p_stdout[READ]);
            else
                *outfp = p_stdout[READ];

            close(p_stdin[READ]);
            close(p_stdout[WRITE]);
            return pid;
        }
    }
}
